/*
 * Copyright 2008 Andrej Herich
 * 
 * http://azetclient.yw.sk/
 * http://azetclient.googlecode.com/
 * 
 * This file is part of AzetClient.
 * 
 * AzetClient is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * AzetClient is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with AzetClient.  If not, see <http://www.gnu.org/licenses/>.
 */

package sk.yw.azetclient.gui;

import java.awt.MenuItem;
import java.awt.PopupMenu;
import javax.swing.SwingUtilities;
import sk.yw.azetclient.gui.actions.ExitAction;
import sk.yw.azetclient.gui.actions.NextConversationAction;

/**
 *
 * @author error216
 */
public class TrayMenu extends PopupMenu
        implements ConversationFocusQueueListener {
    
    private static final long serialVersionUID = 1144639109l;
    
    private ChangeMainFrameVisibilityMenuItem changeMainFrameVisibility;
    private MenuItem nextConversationMenuItem;
    
    public TrayMenu(MainFrame mainFrame) {
        super();
        
        changeMainFrameVisibility =
                new ChangeMainFrameVisibilityMenuItem(true);
        mainFrame.addWindowListener(changeMainFrameVisibility);
        add(changeMainFrameVisibility);
        
        NextConversationAction nextConversationAction =
                new NextConversationAction(
                mainFrame.getConversationFocusQueue());
        String nextConversationActionName =
                (String) nextConversationAction.getValue(
                NextConversationAction.NAME);
        nextConversationMenuItem = new MenuItem(nextConversationActionName);
        nextConversationMenuItem.addActionListener(nextConversationAction);
        nextConversationMenuItem.setEnabled(false);
        add(nextConversationMenuItem);
        mainFrame.getConversationFocusQueue()
                .addConversationFocusQueueListener(this);
        
        ExitAction exitAction = new ExitAction();
        String exitActionName =
                (String) exitAction.getValue(ExitAction.NAME);
        MenuItem exitItem = new MenuItem(exitActionName);
        exitItem.addActionListener(exitAction);
        add(exitItem);
    }

    public void queueIsEmpty() {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                nextConversationMenuItem.setEnabled(false);
            }
        });
    }

    public void queueIsNotEmpty() {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                nextConversationMenuItem.setEnabled(true);
            }
        });
    }

    public ChangeMainFrameVisibilityMenuItem getChangeMainFrameVisibility() {
        return changeMainFrameVisibility;
    }

}
